import 'package:built_collection/built_collection.dart';
import 'package:equatable/equatable.dart';
import 'package:backend.core/core/domain/entities/api/app_store/api_app_store_purchased_subscription.dart';
import 'package:backend.purchases/infrastructure/dtos/api/api_time_dto.dart';
import 'package:backend.purchases/infrastructure/dtos/api/app_store/api_app_store_purchased_item_dto.dart';
import 'package:backend.purchases/infrastructure/dtos/api/app_store/api_app_store_purchased_subscription_price_increase_status_dto.dart';
import 'package:backend.purchases/infrastructure/dtos/api/app_store/api_app_store_purchased_subscription_status_dto.dart';
import 'package:backend.purchases/infrastructure/dtos/api/app_store/api_app_store_purchased_subscription_transaction_dto.dart';
import 'package:backend.purchases/infrastructure/dtos/api/app_store/api_app_store_purchased_subscription_expiration_reason_dto.dart';

const kApiAppStorePurchasedSubscriptionOriginalTransactionIdKey = 'original_transaction_id';
const kApiAppStorePurchasedSubscriptionAutoRenewalEnabledKey = 'auto_renewal_status';
const kApiAppStorePurchasedSubscriptionStatusKey = 'status';
const kApiAppStorePurchasedSubscriptionPriceIncreaseStatusKey = 'price_increase_status';
const kApiAppStorePurchasedSubscriptionExpirationReasonKey = 'expiration_reason';
const kApiAppStorePurchasedSubscriptionGracePeriodExpirationTimeKey =
    'grace_period_expiration_time';
const kApiAppStorePurchasedSubscriptionExpirationTimeKey = 'expiration_time';
const kApiAppStorePurchasedSubscriptionTransactionsKey = 'transactions';

const kApiAppStorePurchasedSubscriptionTypeValue = 'purchased_subscription';

class ApiAppStorePurchasedSubscriptionDto extends Equatable implements ApiAppStorePurchasedItemDto {
  const ApiAppStorePurchasedSubscriptionDto._internal({
    required this.purchaseItemSku,
    required this.originalTransactionId,
    required this.autoRenewalEnabled,
    required this.statusDto,
    required this.priceIncreaseStatusDto,
    required this.test,
    required this.expirationReasonDto,
    required this.gracePeriodExpirationTimeDto,
    required this.expirationTimeDto,
    required this.transactionDtos,
  });

  @override
  final String purchaseItemSku;

  @override
  final bool test;

  final String originalTransactionId;
  final bool autoRenewalEnabled;
  final ApiAppStorePurchasedSubscriptionStatusDto statusDto;
  final ApiAppStorePurchasedSubscriptionPriceIncreaseStatusDto priceIncreaseStatusDto;
  final ApiAppStorePurchasedSubscriptionExpirationReasonDto? expirationReasonDto;
  final ApiTimeDto? gracePeriodExpirationTimeDto;
  final ApiTimeDto? expirationTimeDto;
  final BuiltList<ApiAppStorePurchasedSubscriptionTransactionDto> transactionDtos;

  @override
  List<Object?> get props {
    return [
      purchaseItemSku,
      originalTransactionId,
      autoRenewalEnabled,
      statusDto,
      priceIncreaseStatusDto,
      test,
      expirationReasonDto,
      gracePeriodExpirationTimeDto,
      expirationTimeDto,
      transactionDtos,
    ];
  }

  factory ApiAppStorePurchasedSubscriptionDto.fromEntity(
      ApiAppStorePurchasedSubscription purchasedSubscription) {
    final statusDto =
        ApiAppStorePurchasedSubscriptionStatusDto.fromEntity(purchasedSubscription.status);

    final priceIncreaseStatusDto =
        ApiAppStorePurchasedSubscriptionPriceIncreaseStatusDto.fromEntity(
            purchasedSubscription.priceIncreaseStatus);

    ApiAppStorePurchasedSubscriptionExpirationReasonDto? expirationReasonDto;

    if (purchasedSubscription.expirationReason != null) {
      expirationReasonDto = ApiAppStorePurchasedSubscriptionExpirationReasonDto.fromEntity(
          purchasedSubscription.expirationReason!);
    }

    ApiTimeDto? gracePeriodExpirationTimeDto;

    if (purchasedSubscription.gracePeriodExpirationTime != null) {
      gracePeriodExpirationTimeDto =
          ApiTimeDto.fromEntity(purchasedSubscription.gracePeriodExpirationTime!);
    }

    ApiTimeDto? expirationTimeDto;

    if (purchasedSubscription.expirationTime != null) {
      expirationTimeDto = ApiTimeDto.fromEntity(purchasedSubscription.expirationTime!);
    }

    final transactionDtos = purchasedSubscription.transactions.map((transaction) {
      return ApiAppStorePurchasedSubscriptionTransactionDto.fromEntity(transaction);
    }).toBuiltList();

    return ApiAppStorePurchasedSubscriptionDto._internal(
      purchaseItemSku: purchasedSubscription.purchaseItemSku,
      originalTransactionId: purchasedSubscription.originalTransactionId,
      autoRenewalEnabled: purchasedSubscription.autoRenewalEnabled,
      statusDto: statusDto,
      priceIncreaseStatusDto: priceIncreaseStatusDto,
      test: purchasedSubscription.test,
      expirationReasonDto: expirationReasonDto,
      gracePeriodExpirationTimeDto: gracePeriodExpirationTimeDto,
      expirationTimeDto: expirationTimeDto,
      transactionDtos: transactionDtos,
    );
  }

  @override
  Map<String, dynamic> toJson() {
    final transactionJsons = transactionDtos.map((transactionDto) {
      return transactionDto.toJson();
    }).toList();

    return {
      kApiAppStorePurchasedItemTypeKey: kApiAppStorePurchasedSubscriptionTypeValue,
      kApiAppStorePurchasedItemPurchaseItemSkuKey: purchaseItemSku,
      kApiAppStorePurchasedItemTestKey: test,
      kApiAppStorePurchasedSubscriptionOriginalTransactionIdKey: originalTransactionId,
      kApiAppStorePurchasedSubscriptionAutoRenewalEnabledKey: autoRenewalEnabled,
      kApiAppStorePurchasedSubscriptionStatusKey: statusDto.toString(),
      kApiAppStorePurchasedSubscriptionPriceIncreaseStatusKey: priceIncreaseStatusDto.toString(),
      kApiAppStorePurchasedSubscriptionExpirationReasonKey: expirationReasonDto?.toString(),
      kApiAppStorePurchasedSubscriptionGracePeriodExpirationTimeKey:
          gracePeriodExpirationTimeDto?.toString(),
      kApiAppStorePurchasedSubscriptionExpirationTimeKey: expirationTimeDto?.toString(),
      kApiAppStorePurchasedSubscriptionTransactionsKey: transactionJsons,
    };
  }
}
